#coding=utf-8
#Boa:Dialog:DlgProfit

import wx

from product import CDataSaleOrder,CDataProduct
from MyListCtrl import MyListCtrlProfit


def create(parent):
    return DlgProfit(parent)

[wxID_DLGPROFIT, wxID_DLGPROFITCOMBOWAY, wxID_DLGPROFITTBILLID, 
 wxID_DLGPROFITTCBILLID, wxID_DLGPROFITTCPEER, wxID_DLGPROFITTCPROFIT, 
 wxID_DLGPROFITTCTOTAL, wxID_DLGPROFITTCUSTOMER, wxID_DLGPROFITTPROFIT, 
 wxID_DLGPROFITTTOTAL, wxID_DLGPROFITTWAY, 
] = [wx.NewId() for _init_ctrls in range(11)]

class DlgProfit(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGPROFIT, name=u'DlgProfit',
              parent=prnt, pos=wx.Point(253, 212), size=wx.Size(850, 495),
              style=wx.DEFAULT_DIALOG_STYLE,
              title=u'\u8ba2\u5355\u5229\u6da6\u6838\u7b97')
        self.SetClientSize(wx.Size(842, 461))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))

        self.tBillId = wx.StaticText(id=wxID_DLGPROFITTBILLID,
              label=u'\u8ba2\u5355\u53f7\uff1a', name=u'tBillId', parent=self,
              pos=wx.Point(16, 24), size=wx.Size(68, 16), style=0)

        self.tcBillId = wx.TextCtrl(id=wxID_DLGPROFITTCBILLID, name=u'tcBillId',
              parent=self, pos=wx.Point(88, 16), size=wx.Size(184, 26), style=0,
              value=u'')
        self.tcBillId.Enable(False)
        self.tcBillId.SetEditable(False)

        self.tCustomer = wx.StaticText(id=wxID_DLGPROFITTCUSTOMER,
              label=u'\u5ba2\u6237\uff1a', name=u'tCustomer', parent=self,
              pos=wx.Point(280, 24), size=wx.Size(51, 16), style=0)

        self.tcPeer = wx.TextCtrl(id=wxID_DLGPROFITTCPEER, name=u'tcPeer',
              parent=self, pos=wx.Point(336, 16), size=wx.Size(184, 26),
              style=0, value=u'')
        self.tcPeer.Enable(False)
        self.tcPeer.SetEditable(False)

        self.tTotal = wx.StaticText(id=wxID_DLGPROFITTTOTAL,
              label=u'\u91d1\u989d\uff1a', name=u'tTotal', parent=self,
              pos=wx.Point(16, 56), size=wx.Size(51, 16), style=0)

        self.tcTotal = wx.TextCtrl(id=wxID_DLGPROFITTCTOTAL, name=u'tcTotal',
              parent=self, pos=wx.Point(88, 48), size=wx.Size(100, 26), style=0,
              value=u'')
        self.tcTotal.SetEditable(False)
        self.tcTotal.Enable(False)

        self.tWay = wx.StaticText(id=wxID_DLGPROFITTWAY,
              label=u'\u6838\u7b97\u65b9\u5f0f\uff1a', name=u'tWay',
              parent=self, pos=wx.Point(384, 56), size=wx.Size(85, 16),
              style=0)

        self.comboWay = wx.ComboBox(choices=[], id=wxID_DLGPROFITCOMBOWAY,
              name=u'comboWay', parent=self, pos=wx.Point(472, 48),
              size=wx.Size(150, 24), style=0, value=u'')
        self.comboWay.SetLabel(u'')
        self.comboWay.Bind(wx.EVT_COMBOBOX, self.OnComboWayCombobox,
              id=wxID_DLGPROFITCOMBOWAY)

        self.tProfit = wx.StaticText(id=wxID_DLGPROFITTPROFIT,
              label=u'\u6bdb\u5229\uff1a', name=u'tProfit', parent=self,
              pos=wx.Point(208, 56), size=wx.Size(51, 16), style=0)

        self.tcProfit = wx.TextCtrl(id=wxID_DLGPROFITTCPROFIT, name=u'tcProfit',
              parent=self, pos=wx.Point(264, 48), size=wx.Size(100, 26),
              style=0, value=u'')
        self.tcProfit.SetEditable(False)
        self.tcProfit.Enable(False)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.byPurPrice = u'按最近采购价格'
        self.byBasePrice = u'按最近入库价格'
        self.byAvgPrice = u'按平均价格'
        
        self.comboWay.Append(self.byPurPrice)
        self.comboWay.Append(self.byBasePrice)
        self.comboWay.Append(self.byAvgPrice)
        self.comboWay.SetValue(self.byPurPrice)
        
        self.lc = MyListCtrlProfit(self)
        self.lc.SetSize(wx.Size(820, 300))
        self.lc.Move(wx.Point(16,90))

    def setData(self, billId):
        billObj = self.m_billObj = CDataSaleOrder.findObj(billId)
        
        self.tcBillId.SetValue(billId)
        self.tcPeer.SetValue(billObj.getPeerName())
        self.tcTotal.SetValue(str(billObj.getValue(billObj.COL_TOTAL)))
        self.refreshLcDetail()
        
    def refreshLcDetail(self, priceType='pur'):
        if self.tcBillId.GetValue():
            self.lcDetail = self.lc.refresh({'billId':self.tcBillId.GetValue(),
                                                        'cost':priceType})
        
    def OnComboWayCombobox(self, event):
        if self.comboWay.GetValue() == self.byPurPrice:
            self.refreshLcDetail()
        else:
            msg = u'数据不全，没有增加该功能，请联系开发者'
            dlg = wx.MessageDialog(self, msg, u'提示消息', wx.OK|wx.ICON_INFORMATION)
            dlg.ShowModal()
